package com.tengju.bff.interfaces.shared.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContextUtil {

    private static ThreadLocal<UserServletContext> contextThreadLocal = new ThreadLocal<>();


    private ServletContextUtil() {
    }

    public static void setServletContext(UserServletContext uServletContext) {
        contextThreadLocal.set(uServletContext);
    }

    /**
     * 获取ServletContext
     *
     * @return
     */
    public static UserServletContext getServletContext() {
        return contextThreadLocal.get();
    }

    /**
     * 手动移除ServletContext
     */
    public static void removeServletContext() {
        contextThreadLocal.remove();
    }

    /**
     * 获取request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        UserServletContext context = getServletContext();
        return null == context ? null : context.getRequest();
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        UserServletContext context = getServletContext();
        return null == context ? null : context.getResponse();

    }

}
